package com.oa.controller;

import com.oa.bean.PageInfo;
import com.oa.bean.Job;
import com.oa.service.JobService;
import com.oa.service.JobServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

//使用JobController来接收和处理job模块的所有请求操作
//目的：一个请求处理类映射多个uri
@WebServlet(urlPatterns = {"/insertJob","/deleteJob","/queryJob","/toUpdateJob","/updateJob"})
public class JobServlet extends HttpServlet {

    //获取业务层接口的对象
    JobService jobService = new JobServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");//请求对象设置编码
        //获取请求uri
        String uri = request.getRequestURI();
        System.out.println(uri);
        //response.getWriter().append("JobController--"+uri);
        if(uri.contains("queryJob")){
            queryJob(request,response);     //查询请求处理方法
        }else if(uri.contains("insertJob")){
            insertJob(request,response);   //添加请求处理方法
        }
        else if(uri.contains("toUpdateJob")){//跳转修改页面请求
            toUpdateJob(request,response); //修改页面请求
        }
        else if(uri.contains("updateJob")){
            updateJob(request,response); //修改请求
        }else if(uri.contains("deleteJob")){
            deleteJob(request,response); //删除请求
        }
    }

    private void insertJob(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        //设置响应编码为gbk
        request.setCharacterEncoding("UTF-8");//请求对象设置编码
        //1、从请求对象中获取请求参数 通过getParameter方法根据key来获取value
        String name = request.getParameter("name");
        String remark = request.getParameter("remark");
        System.out.println("name:"+name+",remark:"+remark);
        //创建用户将数据封装
        Job job = new Job();
        job.setName(name);job.setRemark(remark);
        // 2、调用业务层实现功能
        boolean isok = jobService.insertJob(job);
        //3、做出响应--跳转页面
        if(isok){//成功跳转首页
            request.getRequestDispatcher("queryJob").forward(request,response);
        }else{
            request.getRequestDispatcher("JobInsert.jsp").forward(request,response);
        }
    }

    // 只能删除没有被其他表外键引用的
    private void deleteJob(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1、从请求对象中获取请求参数 通过getParameter方法根据key来获取value
        String strId = request.getParameter("id");
        int id = Integer.parseInt(strId);
        System.out.println(id);
        // 2、调用业务层实现功能
        boolean isOk = true;
        try {
            isOk = jobService.deleteJob(id);
        }catch(Exception e){
            e.printStackTrace();
            isOk = false;
        }finally {
            request.setAttribute("info", "删除岗位[id="+id+"]情况：" + (!isOk ? "失败" : "成功"));
            // 3、重新查询
            request.getRequestDispatcher("queryJob").forward(request, response);
        }
    }

    private void updateJob(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置响应编码为gbk
        request.setCharacterEncoding("UTF-8");//请求对象设置编码
        //1、从请求对象中获取请求参数 通过getParameter方法根据key来获取value
        String strId = request.getParameter("id");
        int id = Integer.parseInt(strId);
        String name = request.getParameter("name");
        String remark = request.getParameter("remark");
        Job job = new Job();
        job.setId(id);job.setName(name);job.setRemark(remark);
        //创建用户将数据封装
        // 2、调用业务层实现功能
        boolean isok = jobService.updateJob(job);
        // 3、做出响应--跳转页面
        if(isok){//成功重新查询
            request.getRequestDispatcher("queryJob").forward(request,response);
        }else{//失败跳转修改页面
            request.getRequestDispatcher("toUpdateJob?id"+id).forward(request,response);
        }
    }

    private void queryJob(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        //设置响应编码为gbk
        // response.setCharacterEncoding("GBK");
        String pn = request.getParameter("pn");
        String size = request.getParameter("pageSize");
        String search = request.getParameter("name") == null ? "" : request.getParameter("name");
        int totalJobNum = 0;
        int pageNo = 1,pageSize = 4;
        if(pn!=null&&!pn.equals("")){
            pageNo = Integer.parseInt(pn);
        }
        if(size!=null&&!size.equals("")){
            pageSize = Integer.parseInt(size);
        }
        totalJobNum = jobService.queryJobCount(search);
        if(totalJobNum>0&&totalJobNum<=pageSize){
                pageSize = totalJobNum;
        }else if(totalJobNum==0){
            pageSize=1;
        }
        PageInfo pageInfo  = new PageInfo(pageNo, totalJobNum, pageSize);
        List<Job> jobList = jobService.queryJob(pageInfo,search);
        // 2.保存数据到作用域中
        request.setCharacterEncoding("UTF-8");//请求对象设置编码
        request.setAttribute("jobList", jobList);
        request.setAttribute("pageInfo", pageInfo);
        if(search !=null&&! search.equals(""))
            request.setAttribute("matchJobName",search);
        else
            request.setAttribute("matchJobName","");
        //转发是请求链没有断开的
        request.getRequestDispatcher("JobIndex.jsp").forward(request,response);
    }

    private void toUpdateJob(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取ID,根据id来查询
        String jobId = request.getParameter("id");
        int id = Integer.parseInt(jobId);
        Job job = jobService.queryJobById(id);
        System.out.println(job);
        //2.保存数据
        request.setAttribute("job",job);
        //3.跳转页面
        request.getRequestDispatcher("JobUpdate.jsp").forward(request,response);
    }
}
